package labmanager.business;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import dto.ResponseDTO;

public class LabManagerReservation {

	private static Logger LOGGER = Logger.getLogger(LabManagerReservation.class.getName());
	
	/**
	 * @param dateReservation
	 * @param typeReservation
	 * @param timeReservation
	 * @param userId
	 * @return
	 */
	public ResponseDTO createReservation(String dateReservation,
			int typeReservation, int timeReservation, String state, int userId) {
		try {
			labmanager.database.LabManagerReservation labmanager = new labmanager.database.LabManagerReservation();
			
			DateFormat format = new SimpleDateFormat ("yyyy/MM/dd hh:mm");
			Date date = (Date)format.parse(dateReservation);
			SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dateReservation = formato.format(date);
			
			return labmanager.createReservation(dateReservation, typeReservation, timeReservation, state, userId);
		} catch (Exception e) {
			LOGGER.debug(new StringBuilder("Se produjo un error en business/createReservation al registrar la reserva. ")
					.append(e).toString(), e);
			ResponseDTO response = new ResponseDTO();
			response.setCode(99);
			response.setMessage("Se ha producido un error.");
			return response;
		}
	}

	public ResponseDTO updateReservation(int id, String dateReservation,
			int typeReservation, int timeReservation, String state) {
		try {
			labmanager.database.LabManagerReservation labmanager = new labmanager.database.LabManagerReservation();
			
			DateFormat format = new SimpleDateFormat ("yyyy/MM/dd hh:mm");
			Date date = (Date)format.parse(dateReservation);
			SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dateReservation = formato.format(date);
			
			return labmanager.updateReservation(id, dateReservation, typeReservation, timeReservation, state);
		} catch (Exception e) {
			LOGGER.debug(new StringBuilder("Se produjo un error en business/createReservation al registrar la reserva. ")
					.append(e).toString(), e);
			ResponseDTO response = new ResponseDTO();
			response.setCode(99);
			response.setMessage("Se ha producido un error.");
			return response;
		}
	}

}
